<template>
  <div class="wrapper">
      <img src="http://www.dell-lee.com/imgs/vue3/user.png" class="wrapper__img" />
      <div class="wrapper__input">
        <input
        class="wrapper__input__content"
        placeholder="请输入用户名"
        v-model="username"
        />
      </div>
      <div class="wrapper__input">
        <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
        v-model="password"
        />
      </div>
      <div class="wrapper__input">
        <input
        type="password"
        class="wrapper__input__content"
        placeholder="确认密码"
        autocomplete="new-password"
        v-model="ensurement"
        />
      </div>
      <div class="wrapper__register-button" @click="handleRegister">注册</div>
      <div class="wrapper__register-link" @click="handleLoginClick">已有帐号去登录</div>
      <Toast v-if="show" :message="toastMessage" />
  </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { post } from '../../utils/request'
import Toast, { useToastEffect } from '../../components/Toast'

// 处理注册相关逻辑
const useRegisterEffect = (showToast) => {
  const router = useRouter()
  const data = reactive({
    username: '',
    password: '',
    ensurement: ''
  })

  const handleRegister = async () => {
    try {
      const { username, password, ensurement } = data
      if (username.trim().length === 0 || password.trim().length === 0 || ensurement.trim().lenght === 0) {
        showToast('请输入用户名或密码')
        return false
      }
      if (ensurement !== password) {
        showToast('密码与确认密码不一致')
        return false
      }
      // await等待promise 对象执行完毕，然后拿到promise resolve 的值并进行返回，返回值拿到之后，它继续向下执行(用同步思维解决异步问题的方案)
      const result = await post('/api/user/register', {
        username: data.username,
        password: data.password
      })
      console.log(result)
      if (result?.errno === 0) {
        router.push({ name: 'Login' }) // 页面跳转
      } else {
        showToast('注册失败')
      }
    } catch (e) {
      showToast('请求失败')
    }
  }
  const { username, password, ensurement } = toRefs(data)
  return { username, password, ensurement, handleRegister }
}

// 处理登陆跳转
const useLoginEffect = () => {
  const router = useRouter()
  const handleLoginClick = () => {
    router.push({ name: 'Login' })
  }
  return { handleLoginClick }
}

export default {
  name: 'Register',
  components: { Toast },
  setup () {
    // 弹窗逻辑
    const { show, toastMessage, showToast } = useToastEffect()
    // 注册逻辑
    const { username, password, ensurement, handleRegister } = useRegisterEffect(showToast)
    // 登录跳转
    const { handleLoginClick } = useLoginEffect()

    return { username, password, ensurement, show, toastMessage, handleRegister, handleLoginClick }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables.scss';
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%); // 这个元素里高度的50%
  &__img {
    display: block;
    width: .66rem;
    height: .66rem;
    margin: 0 auto .4rem auto;
  }
  &__input {
    height: .48rem;
    margin: 0 .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #F9F9F9;
    border: .01rem solid rgba(0,0,0,0.10);
    border-radius: .06rem;
    &__content {
      margin-top: .12rem;
      line-height: .22rem;
      border: none;
      outline: none;
      width: 100%;
      background: none;
      font-size: .16rem;
      color: $content-notice-fontcolor;
      &::placeholder { // 修饰符
        color: $content-notice-fontcolor;
      }
    }
  }
  &__register-button {
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: $btn-bgColor;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32); // 素添加阴影：
    border-radius: .04rem;
    color: $bgColor;;
    font-size: .16rem;
    text-align: center;
  }
  &__register-link {
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
